package com.zj.fx.controller.admin;

import com.zj.fx.model.month.MonthResult;
import com.zj.fx.service.month.MonthResultService;
import com.zj.fx.util.SystemExportTools;
import lombok.RequiredArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 23562
 */
@Controller
@RequestMapping("AgencyStyleExportController")
@RequiredArgsConstructor
public class AgencyStyleExportController {

    @Value("${file.templatePath}")
    private String templatePaths;

    @Autowired
    private MonthResultService monthResultService;

    /**
     * 营销部管理员导出实时结果数据
     *
     * @param request
     * @param response
     * @param monthResult
     * @throws IOException
     */
    @RequestMapping("agencyStyleExport1")
    public void agencyStyleExport1(HttpServletRequest request, HttpServletResponse response, MonthResult monthResult) throws IOException {
        // 创建sheet
        String templatePath = templatePaths + "/monthresultcurrent.xls";
//        String templatePath = "G:\\agencyStyleExport1.xls";
        FileInputStream fileInputStream = new FileInputStream(templatePath);
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFFont font = workbook.createFont();

        Map<Integer, HSSFCellStyle> styleMap = SystemExportTools.indexStyle(workbook, font);
        Map<Integer, HSSFCellStyle> styleMapRed = SystemExportTools.indexStyleRed(workbook, font);
        List<MonthResult> monthResults = monthResultService.list4Score(monthResult);
        monthResults = monthResults.stream().sorted(Comparator.comparing(MonthResult::getTotalScore).reversed()).collect(Collectors.toList());
        //结果未发布前最终排名未确定，排序赋值
        for (int i = 1; i <= monthResults.size(); i++) {
            monthResults.get(i-1).setFinalrank(String.valueOf(i));
        }

        //创建行
        HSSFSheet sheet = workbook.getSheetAt(0);
//        for (int i = 0; i < monthResults.size(); i++) {
//            Row row = sheet.createRow((i + 2));
//        }

        //常见单元格并写入数据
        for (int i = 0; i < monthResults.size(); i++) {

            Integer star = Integer.valueOf(monthResults.get(i).getStar().toString());

            Row row = sheet.createRow((i + 1));

            //排名
            Cell cell = row.createCell(0);
            cell.setCellValue(monthResults.get(i).getFinalrank());
            cell.setCellStyle(styleMap.get(0));
            //任务年月
            cell = row.createCell(1);
            cell.setCellValue(monthResults.get(i).getTaskMonth());
            cell.setCellStyle(styleMap.get(0));
            //供电所名称
            cell = row.createCell(2);
            cell.setCellValue(monthResults.get(i).getSupplyName());
            cell.setCellStyle(styleMap.get(0));
            //所属分公司
            cell = row.createCell(3);
            cell.setCellValue(monthResults.get(i).getOrgnizationName());
            cell.setCellStyle(styleMap.get(0));
            //供电所星级
            cell = row.createCell(4);
            cell.setCellValue(star);
            cell.setCellStyle(styleMap.get(0));
            //一、营销服务
            Double yingxiaoScore = Double.valueOf(monthResults.get(i).getYingxiaoScore().toString());
            cell = row.createCell(5);
            cell.setCellValue(yingxiaoScore);
            starRed(star, yingxiaoScore, cell, styleMapRed, styleMap);
            //二、运维抢修
            Double yunjianScore = Double.valueOf(monthResults.get(i).getYunjianScore().toString());
            cell = row.createCell(6);
            cell.setCellValue(yunjianScore);
            starRed(star, yunjianScore, cell, styleMapRed, styleMap);
            //三、安全管理
            Double anquanScore = Double.valueOf(monthResults.get(i).getAnquanScore().toString());
            cell = row.createCell(7);
            cell.setCellValue(anquanScore);
            starRed(star, anquanScore, cell, styleMapRed, styleMap);
            //四、党建及队伍建设
            Double dangjianScore = Double.valueOf(monthResults.get(i).getDangjianScore().toString());
            cell = row.createCell(8);
            cell.setCellValue(dangjianScore);;
            starRed(star, dangjianScore, cell, styleMapRed, styleMap);
            //五、数字化转型
            Double shuzihuaScore = Double.valueOf(monthResults.get(i).getShuzihuaScore().toString());
            cell = row.createCell(9);
            cell.setCellValue(shuzihuaScore);
            starRed(star, shuzihuaScore, cell, styleMapRed, styleMap);
            //六、发展规划
            Double fazhanScore = Double.valueOf(monthResults.get(i).getFazhanScore().toString());
            cell = row.createCell(10);
            cell.setCellValue(fazhanScore);
            starRed(star, fazhanScore, cell, styleMapRed, styleMap);
            //七、“五小”建设及基础保障
            Double wuxiaoScore = Double.valueOf(monthResults.get(i).getWuxiaoScore().toString());
            cell = row.createCell(11);
            cell.setCellValue(wuxiaoScore);
            starRed(star, wuxiaoScore, cell, styleMapRed, styleMap);
            //总得分（未加权）
            cell = row.createCell(12);
            cell.setCellValue(monthResults.get(i).getTotalMark().toString());
            cell.setCellStyle(styleMap.get(0));
            //总得分（权重计算）
            cell = row.createCell(13);
            cell.setCellValue(monthResults.get(i).getTotalScore().toString());
            cell.setCellStyle(styleMap.get(0));
            //必备条件是否合格
            cell = row.createCell(14);
            cell.setCellValue(monthResults.get(i).getIsNeccessary().equals("1")?"是":"否");
            cell.setCellStyle(monthResults.get(i).getIsNeccessary().equals("1")?styleMap.get(0):styleMapRed.get(0));
            //结果是否发布
            cell = row.createCell(15);
            cell.setCellValue(monthResults.get(i).getIsIssue().equals("1")?"是":"否");
            cell.setCellStyle(styleMap.get(0));

        }

        String fileName = "111.xls";
        SystemExportTools.commonDownload(request, response, workbook, fileName);
    }

    private static void starRed(Integer star, Double yingxiaoScore, Cell cell, Map<Integer, HSSFCellStyle> styleMapRed, Map<Integer, HSSFCellStyle> styleMap) {
        if (star == 5){
            if (yingxiaoScore <90){
                cell.setCellStyle(styleMapRed.get(0));
            }else {
                cell.setCellStyle(styleMap.get(0));
            }
        } else if (star == 4){
            if (yingxiaoScore <85){
                cell.setCellStyle(styleMapRed.get(0));
            }else {
                cell.setCellStyle(styleMap.get(0));
            }
        } else if (star == 3){
            if (yingxiaoScore <80){
                cell.setCellStyle(styleMapRed.get(0));
            }else {
                cell.setCellStyle(styleMap.get(0));
            }
        }
    }

}
